package com.relytosoft.mizar.analyze.system.services.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.system.api.entity.AyPersonGroup;
import com.relytosoft.mizar.analyze.system.api.entity.AyPersonInfo;
import com.relytosoft.mizar.analyze.system.api.pojo.dto.AyPersonInfoDto;
import com.relytosoft.mizar.analyze.system.api.pojo.vo.AyPersonInfoVo;
import com.relytosoft.mizar.analyze.system.services.service.AyPersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/13 15:55
 */
@Api(tags = {"人脸信息"})
@RestController
@RequestMapping("/system/person")
public class AyPersonInfoController {

    @Resource
    private AyPersonInfoService ayPersonInfoService;

    @ApiOperation("人脸查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(@RequestParam("condition") String condition,
                                                    @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyPersonInfoDto ayPersonInfoDto = JSONObject.parseObject(condition, AyPersonInfoDto.class);
        AyPersonInfo ayPersonInfo = BeanCopyUtils.copy(ayPersonInfoDto, AyPersonInfo.class);
        IPage<AyPersonInfo> page = ayPersonInfoService.selectAyPersonInfoPageList(pageQuery, ayPersonInfo);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("新增人脸")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody AyPersonInfoDto ayPersonInfoDto) {
        AyPersonInfo ayPersonInfo = BeanCopyUtils.copy(ayPersonInfoDto, AyPersonInfo.class);
        Map<String, Object> map = ayPersonInfoService.addAyPersonInfo(ayPersonInfo);
        List<String> plateformGroupIds = (List<String>) map.get("plateformGroupIds");
        String base64Image = (String) map.get("base64Image");
        ayPersonInfoService.sendAddToKafka(plateformGroupIds, base64Image, ayPersonInfo.getId());
        if (!map.isEmpty()) {
            return ResponseResult.ok("新增成功");
        } else {
            return ResponseResult.fail("新增失败");
        }
    }

    @ApiOperation("人脸详情")
    @GetMapping({"{personId}"})
    public ResponseResult<AyPersonInfoVo> id(@PathVariable Integer personId) {
        return ResponseResult.ok(ayPersonInfoService.selectById(personId));
    }


    @ApiOperation("修改人脸信息")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyPersonInfoDto ayPersonInfoDto) {
        AyPersonInfo ayPersonInfo = BeanCopyUtils.copy(ayPersonInfoDto, AyPersonInfo.class);
        //数据操作
        JSONObject jsonObject = ayPersonInfoService.updateAyPersonInfo(ayPersonInfo);
        //消息
        if (jsonObject != null) {
            List<AyPersonGroup> personGroups = JSONObject.parseObject(jsonObject.getString("personGroups"), new TypeReference<List<AyPersonGroup>>() {
            });
            List<String> plateformGroupIds = JSONObject.parseObject(jsonObject.getString("plateformGroupIds"), new TypeReference<List<String>>() {
            });
            String base64Image = jsonObject.getString("base64Image");
            ayPersonInfoService.sendRemoveToKafka(personGroups);
            ayPersonInfoService.sendAddToKafka(plateformGroupIds, base64Image, ayPersonInfo.getId());
            return ResponseResult.ok("修改成功");
        } else {
            return ResponseResult.fail("修改失败");
        }
    }

    @ApiOperation("删除人脸信息")
    @DeleteMapping("del")
    public ResponseResult<Void> remove(@RequestBody List<Integer> personIds) {
        //数据库操作
        List<AyPersonGroup> personGroupList = ayPersonInfoService.removeAyPersonInfo(personIds);
        if (!personGroupList.isEmpty()) {
            //删除比特大陆人脸信息
            ayPersonInfoService.sendRemoveToKafka(personGroupList);
            return ResponseResult.ok("删除成功");
        } else {
            return ResponseResult.fail("删除失败");
        }
    }

    @ApiOperation("批量导入")
    @PostMapping("import/{ids}")
    public ResponseResult<Void> batchImport(@RequestPart("file") MultipartFile multipartFile,
                                            @PathVariable("ids") List<Integer> ids) {
        boolean b = ayPersonInfoService.batchImport(multipartFile, ids);
        if (b) {
            return ResponseResult.ok("导入成功");
        } else {
            return ResponseResult.fail("导入失败");
        }

    }
}
